package gat.api.rest.background.dic;

import com.bitsun.core.common.annotation.SkipLogin;
import com.bitsun.core.common.web.ResultDTO;
import gat.RedisService;
import gat.api.background.dic.BDicDataApi;
import gat.application.dic.DicDataService;
import gat.convertor.dic.DicDataResDtoConvertor;
import gat.dto.req.dic.DicDataReqDto;
import gat.dto.res.dic.DicDataListReqDto;
import gat.dto.res.dic.DicDataResDto;
import gat.dto.res.dic.DicDataVo;
import gat.infrastructure.persistence.dao.dic.DicDataMapper;
import gat.infrastructure.persistence.po.dic.DicDataPo;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

/**
 * 
 * @Author: tmac
 * @Email: tmac
 */
@Slf4j
@SuppressWarnings("unchecked")
@RestController
@RequestMapping("/dicData")
public class BDicDataApiController implements BDicDataApi {


    @Autowired
    private DicDataService dicDataService;
    @Autowired
    private DicDataMapper dicDataMapper;
    @Autowired
    private DicDataResDtoConvertor dicDataResDtoConvertor;

    @Override
    @GetMapping(value = "/getByType/{typeCode}", produces = {"application/json"})
    @SkipLogin
    public ResultDTO<List<DicDataVo>> getByType(@ApiParam(value = "typeCode", required = true) @PathVariable("typeCode") String typeCode) {
        List<DicDataVo> list = dicDataService.getByTypeCode(typeCode);
        return ResultDTO.ok(list);
    }

    @Autowired
    private RedisService redisService;

    @Override
    @PatchMapping(value = "/{id}", produces = {"application/json"})
    public ResultDTO<DicDataResDto> patchUpdateDicData(@ApiParam(value = "对象ID", required = true) @PathVariable("id") Long id,
                                                       @ApiParam(value = "要修改的对象，对象属性有值的才更新,即null值不更新", required = true) @Valid @RequestBody DicDataReqDto reqDto) {
        dicDataService.updateProps(id, reqDto);
        DicDataResDto resDto = dicDataService.selectOne(id);

        if (StringUtils.equals(resDto.getTypeCode(), "seckillConfig") || StringUtils.equals(resDto.getTypeCode(), "freeActivityConfig")) {
            redisService.del("hao6871:dicData:info:"+resDto.getTypeCode());
        }

        return ResultDTO.ok(resDto);
    }

    @Override
    @PatchMapping(value = "/batchUpdate", produces = {"application/json"})
    public ResultDTO<List<DicDataVo>> batchUpdate(@RequestBody DicDataListReqDto reqDto){
        List<Long> ids = new ArrayList<>();
        for (DicDataReqDto dicDataReqDto : reqDto.getDicDataReqDtos()) {
            dicDataService.updateProps(dicDataReqDto.getId(), dicDataReqDto);
            ids.add(dicDataReqDto.getId());
        }
        List<DicDataPo> dicDataPos = dicDataMapper.selectBatchIds(ids);
        return ResultDTO.ok(dicDataResDtoConvertor.poList2VoList(dicDataPos));
    }

}
